package dao.backstage.impl;

import dao.backstage.BaseDao;
import dao.backstage.GoodsDao;
import pojo.Goods;
import utils.backstage.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl extends BaseDao implements GoodsDao {
    Connection conn = null;
    @Override
    public int addGoods(Goods goods) {
        String sql = "insert into t_goods(goods_name,  picture,  price,  type_id,  detail) values(?,?,?,?,?)";
        int update = update(sql, goods.getGoods_name(), goods.getPicture(), goods.getPrice(), goods.getType_id(), goods.getDetail());
        return update;
    }

    @Override
    public int updateGoods(Goods goods) {
        String sql = "update t_goods set goods_name=?,picture=?,price=?,type_id=?,detail=? where id=?";
        int update = update(sql,goods.getGoods_name(),goods.getPicture(), goods.getPrice(), goods.getType_id(), goods.getDetail(),goods.getId());
        return update;
    }

    @Override
    public int removeGoods(String id) {
        String sql = "delete from t_goods where id=?";
        int update = update(sql,id);
        return update;
    }

    @Override
    public List<Goods> queryByName(String goods_name) throws SQLException {
        List<Goods> list = new ArrayList<>();
        conn = JDBCUtils.getConnection();
        String sql = "select * from t_goods where goods_name=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,goods_name);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            Goods good = new Goods();
            good.setId(rs.getInt("id"));
            good.setGoods_name(rs.getString("goods_name"));
            good.setPicture(rs.getString("picture"));
            good.setPrice(rs.getBigDecimal("price"));
            good.setType_id(rs.getInt("type_id"));
            good.setDetail(rs.getString("detail"));
            list.add(good);
        }
        return list;
    }

    @Override
    public List<Goods> queryByType(int type_id) throws SQLException {
        List<Goods> list = new ArrayList<>();
        conn = JDBCUtils.getConnection();
        String sql = "select * from t_goods where type_id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, String.valueOf(type_id));
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            Goods good = new Goods();
            good.setId(rs.getInt("id"));
            good.setGoods_name(rs.getString("goods_name"));
            good.setPicture(rs.getString("picture"));
            good.setPrice(rs.getBigDecimal("price"));
            good.setType_id(rs.getInt("type_id"));
            good.setDetail(rs.getString("detail"));
            list.add(good);
        }
        return list;
    }

    @Override
    public List<Goods> queryAll() throws SQLException {
        List<Goods> list = new ArrayList<>();
        conn = JDBCUtils.getConnection();
        String sql = "select * from t_goods";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            Goods good = new Goods();
            good.setId(rs.getInt("id"));
            good.setGoods_name(rs.getString("goods_name"));
            good.setPicture(rs.getString("picture"));
            good.setPrice(rs.getBigDecimal("price"));
            good.setType_id(rs.getInt("type_id"));
            good.setDetail(rs.getString("detail"));
            list.add(good);
        }
        return list;
    }
}
